package sicnu.cs.ich.user.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import sicnu.cs.ich.api.domain.form.PageParam;
import sicnu.cs.ich.api.domain.vo.UserVo;
import sicnu.cs.ich.security.domain.entity.Role;
import sicnu.cs.ich.security.mapper.RoleMapper;
import sicnu.cs.ich.security.mapper.UserMapper;
import sicnu.cs.ich.user.service.IAdminService;

import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author 沈洋 邮箱:1845973183@qq.com
 * @create 2021/12/3-18:48
 **/
@Service
@Slf4j
@RequiredArgsConstructor
public class AdminServiceImpl implements IAdminService {
    private final UserMapper userMapper;
    private final RoleMapper roleMapper;

    @Override
    public PageInfo<UserVo> getUsers(PageParam param) {
        PageHelper.startPage(param.getPageNum(), param.getPageSize());
        List<UserVo> users = userMapper.findUserVos(param);
        if (CollectionUtils.isEmpty(users)) {
            throw new NoSuchElementException("无数据");
        }
        return new PageInfo<>(users);
    }

    /**
     * 重置用户密码
     *
     * @param userIds     用户数组
     * @param newPassword 重置后的密码
     * @return 是否成功
     */
    @Override
    public boolean resetUserPassword(Integer[] userIds, String newPassword) {
        return userMapper.updatePassword(userIds, newPassword) > 0;
    }

    /**
     * 获取角色信息
     * @param param 参数
     */
    @Override
    public PageInfo<Role> getRoles(PageParam param) {
        PageHelper.startPage(param.getPageNum(),param.getPageSize());
        var list = roleMapper.findRoles();
        return new PageInfo<>(list);
    }



    /**
     * 修改用户账号状态
     *
     * @param userId    用户id
     * @param lockState 锁定状态
     */
    @Override
    public boolean updateUserState(Integer userId, Integer lockState) {
        return userMapper.updateUserLockState(userId,lockState)>0;
    }

    /**
     * 更新用户角色
     *
     * @param userId  用户id
     */
    @Override
    public boolean updateUserRole(Integer userId, Set<Integer> newRoleIds) {
        var oldRoles = roleMapper.findRolesByUserid(userId);
        if (CollectionUtils.isEmpty(oldRoles)){
            return false;
        }

        // 调用insert or replace
        var oldRoleIds = oldRoles.stream()
                .map(Role::getId).collect(Collectors.toSet());
        // 获取要删除的id
        var deleteRoleIds = oldRoleIds.stream()
                .filter(roleId->{
                    return !newRoleIds.contains(roleId);
                }).collect(Collectors.toList());
        // 获取要添加的id
        var insertRoleIds = newRoleIds.stream()
                .filter(roleId->{
                    return !oldRoleIds.contains(roleId);
                }).collect(Collectors.toList());
        return true;
    }
}
